package com.buka.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buka.constants.SystemConstants;
import com.buka.domain.ResponseResult;
import com.buka.mapper.CategoryMapper;
import com.buka.pojo.Article;
import com.buka.pojo.Category;
import com.buka.service.ArticleService;
import com.buka.service.CategoryService;
import com.buka.util.BeanCopyUtils;
import com.buka.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 26755
 * @description 针对表【category(分类表)】的数据库操作Service实现
 * @createDate 2024-03-21 14:51:34
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {
    @Autowired
    private ArticleService articleService;

    @Override
    public ResponseResult getCategoryList() {
        //查询文章表
        LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        lambdaQueryWrapper.groupBy(Article::getCategoryId);
        lambdaQueryWrapper.select(Article::getCategoryId);
        List<Article> list = articleService.list(lambdaQueryWrapper);
        List<Long> collect = list.stream().map(Article::getCategoryId).collect(Collectors.toList());
        //查询分类表
        List<Category> categories = listByIds(collect);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public List<CategoryVo> listAllCategory() {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus, SystemConstants.NORMAL);
        List<Category> list = list(wrapper);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list, CategoryVo.class);
        return categoryVos;
    }

    @Override
    public ResponseResult list(int pageNum, int pageSize, String name, String status) {
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(name)) {
            categoryLambdaQueryWrapper.like(Category::getName, name);
        }
        if (StringUtils.hasText(status)) {
            categoryLambdaQueryWrapper.eq(Category::getStatus, status);
        }
        Page<Category> page = new Page<>(pageNum, pageSize);
        Page<Category> categoryPage = page(page, categoryLambdaQueryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", categoryPage.getRecords());
        map.put("total", categoryPage.getTotal());
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult addCAtegory(Category category) {
        save(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getCategoryById(int id) {
        Category byId = getById(id);
        return ResponseResult.okResult(BeanCopyUtils.copyBean(byId, CategoryVo.class));
    }

    @Override
    public ResponseResult updateCategoryById(Category category) {
        updateById(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteCategoryById(int id) {
        getBaseMapper().deleteById(id);
        return ResponseResult.okResult();
    }
}




